package top.mataiji.manager.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import top.mataiji.manager.mapper.BookTypeMapper;
import top.mataiji.manager.pojo.BookType;
import top.mataiji.manager.pojo.BookTypeExample;
import top.mataiji.manager.pojo.vo.BookTypeVo;
import top.mataiji.manager.service.BookTypeServe;

import java.util.ArrayList;
import java.util.List;

@Service
public class BookTypeServeImpl implements BookTypeServe {

    @Autowired
    BookTypeMapper bookTypeMapper;

    /**\
     *  查询所用分类
     * @return
     */
    @Override
    public List<BookTypeVo> selectAllType() {

        List<BookTypeVo> bookTypeVos = new ArrayList<>();
        //创建查询对象
        BookTypeExample example = new BookTypeExample();
        //设置查询条件
        example.createCriteria().andParentEqualTo(0);
        //查询
        List<BookType> bookTypes = bookTypeMapper.selectByExample(example);
        bookTypes.forEach(e->{
            BookTypeVo bookTypeVo1 = new BookTypeVo();
            bookTypeVo1.setBookType(e);
            //清空查询条件
            example.clear();
            example.createCriteria().andParentEqualTo(e.getTypeid());
            //查询
            List<BookType> bookTypes1 = bookTypeMapper.selectByExample(example);
            bookTypeVo1.setChildrens(bookTypes1);
            bookTypeVos.add(bookTypeVo1);
        });
        return bookTypeVos;
    }

    @Override
    public List<BookType> selectParent() {
        //创建查询对象
        BookTypeExample example = new BookTypeExample();
        example.createCriteria().andParentEqualTo(0);
        return bookTypeMapper.selectByExample(example);
    }

    @Override
    public List<BookType> selectChilderns(Integer parent) {
        //创建查询对象
        BookTypeExample example = new BookTypeExample();
        example.createCriteria().andParentEqualTo(parent);
        return bookTypeMapper.selectByExample(example);
    }
}
